/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package chatsoft.client.buz;


import ChatSoft.model.ServerInfo;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.Mixer;
import javax.sound.sampled.SourceDataLine;
import javax.sound.sampled.TargetDataLine;

/**
 *
 * @author VinhThien
 */
public class VoiceBusiness extends Thread {

    public ServerSocket server_socket;
    Socket clientSocket = null;
    BufferedInputStream input;
    TargetDataLine targetDataLine;
    BufferedOutputStream out;
    ByteArrayOutputStream byteArrayOutputStream;
    AudioFormat audioFormat;
    SourceDataLine sourceDataLine;
    byte tempBuffer[] = new byte[10000];

    public VoiceBusiness() throws IOException, LineUnavailableException {
        server_socket = new ServerSocket(500);
        audioFormat = getAudioFormat();
        DataLine.Info dataLineInfo = new DataLine.Info(SourceDataLine.class, audioFormat);
        sourceDataLine = (SourceDataLine) AudioSystem.getLine(dataLineInfo);
        sourceDataLine.open(audioFormat);
        sourceDataLine.start();
        captureAudio();
        
    }

    private AudioFormat getAudioFormat() {
        float sampleRate = 5000.0F;
        int sampleSizeInBits = 16;
        int channels = 1;
        boolean signed = true;
        boolean bigEndian = true;
        return new AudioFormat(
                sampleRate,
                sampleSizeInBits,
                channels,
                signed,
                bigEndian);
    }

    private void captureAudio() {
        try {
            Mixer.Info[] mixerInfo = AudioSystem.getMixerInfo();
            System.out.println("Available mixers:");
            for (int cnt = 0; cnt < mixerInfo.length; cnt++) {
                System.out.println(mixerInfo[cnt].getName());
            }
            audioFormat = getAudioFormat();

            DataLine.Info dataLineInfo = new DataLine.Info(
                    TargetDataLine.class, audioFormat);

            Mixer mixer = AudioSystem.getMixer(mixerInfo[3]);

            targetDataLine = (TargetDataLine) mixer.getLine(dataLineInfo);

            targetDataLine.open(audioFormat);
            targetDataLine.start();

        } catch (Exception e) {
            System.out.println(e);
            System.exit(0);
        }
    }

    public void closeSocket() throws IOException {
        if (server_socket != null && !server_socket.isClosed()) {
            server_socket.close();
        }
    }

    @Override
    public void run() {
        try {
            try {
                while (!this.isInterrupted())//luon luon cho ket noi tu phia client
                {
                    clientSocket = server_socket.accept();
                    while (input.read(tempBuffer) != -1) {
                        sourceDataLine.write(tempBuffer, 0, 10000);
                    }
//                    new VoiceController(clientSocket).start();
                }
            } finally {
                closeSocket();
            }
        } catch (IOException ioe) {
//            Logger.getLogger(ServerBussiness.class.getName()).log(Level.SEVERE, null, ioe);
        }
    }
    
    
}
